/**
 * 
 */
package com.attilax.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

/**
 * @author attilax
 *2016年4月14日 下午9:43:32
 */
public class PathUtil4android {
	
	/**
	 * o3i    
	 * @param fileFullPath 
	 */
	public static void createAllPath(String fileFullPath) {
		File file = new File(fileFullPath);
		 if (!file.getParentFile().exists()) {
		     System.out.println("目标文件所在路径不存在，准备创建。。。:"+fileFullPath);
		     if (!file.getParentFile().mkdirs()) {
		      System.out.println("创建目录文件所在的目录失败！");
		   
		     }
		 }
	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		String imageFilePath = "F:\\美国队长2\\美国队长2.jpg";
		imageFilePath="http://localhost:8080/cms/maindir/%E8%A5%BF%E6%B8%B8%E8%AE%B0%E4%B9%8B%E5%A4%A7%E5%9C%A3%E5%BD%92%E6%9D%A5/%E8%A5%BF%E6%B8%B8%E8%AE%B0%E4%B9%8B%E5%A4%A7%E5%9C%A3%E5%BD%92%E6%9D%A5-fanart.jpg";
		imageFilePath=URLDecoder.decode(imageFilePath, "utf8");
		
		System.out.println(PathUtil4android.getPathNohostNoApproot(imageFilePath, "/cms/maindir"));
	
	}
	/**
	 * 获取内置SD卡路径
	 * @return
	 */
	public String getInnerSDCardPath() {  
        return Environment.getExternalStorageDirectory().getPath();  
    }
	
	public static String  getPathNohostNoApproot(String url,String approot)
	{
	 url=deHostPart(url);
	int  idx=url.indexOf( approot);
	String imageFileNoPath=url.substring(idx+approot.length()+1);
	return imageFileNoPath;
	}
	
	/**
	attilax    2016年4月14日  下午10:18:17
	 * @param url
	 * @return
	 */
	private static String deHostPart(String url) {
		url = deProtocal(url);
		url=deHost(url);
		return  url;
	}
	private static String deHost(String url) {
		int idx2=url.indexOf("/");
		url=url.substring(idx2);
		return url;
	}
	private static String deProtocal(String url) {
		int idx=url.indexOf("://");
		url=url.substring(idx+3);
		return url;
	}
	/**
	 * 
	 * 
	 * 打印结果：

1. 插入一张外置SD卡后

内置SD卡路径：/storage/emulated/0
外置SD卡路径：/storage/extSdCard

2. 取出外置SD卡后

内置SD卡路径：/storage/emulated/0


	 * 获取外置SD卡路径
	 * @return	应该就一条记录或空
	 */
	public List getExtSDCardPath()
	{
		List lResult = new ArrayList();
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("mount");
			InputStream is = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line;
			while ((line = br.readLine()) != null) {
				if (line.contains("extSdCard"))
				{
					String [] arr = line.split(" ");
					String path = arr[1];
					File file = new File(path);
					if (file.isDirectory())
					{
						lResult.add(path);
					}
				}
			}
			isr.close();
		} catch (Exception e) {
		}
		return lResult;
	}


}
